package com.zggk.lzz.util;

import android.app.Activity;
import android.content.Context;
import android.location.LocationManager;
import android.util.Base64;
import android.util.DisplayMetrics;

import com.blankj.utilcode.util.StringUtils;

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.IOException;

/**
 * Created by 张成昆 on 2021-3-18.
 */

public class Utils {
    private static Context context;
    /**
     * 初始化工具类
     *
     * @param context 上下文
     */
    public static void init(Context context) {
        Utils.context = context.getApplicationContext();
    }
    public static boolean isNull(String str) {
        if (str != null) {
            str = str.trim();
        }
        if (str == null || "".equals(str)) {
            return true;
        }
        return false;
    }
    public static String bmpToBase64String(String imgPath) {

        // 用io流读取到要上传的图片，用Base64编码成字节流的字符串,得到uploadBuffer（要传到webservice接口上）
        FileInputStream fis = null;
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        String uploadBuffer = "";
        try {
            if (StringUtils.isEmpty(imgPath)) {
                return "";
            }
            fis = new FileInputStream(imgPath);
            byte[] buffer = new byte[16];
            int count = -1;
            while ((count = fis.read(buffer)) >= 0) {
                baos.write(buffer, 0, count);
            }
            // 进行Base64编码得到字符串
            uploadBuffer = Base64.encodeToString(baos.toByteArray(), Base64.NO_WRAP);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return uploadBuffer;
    }
    public static String getZHMCByZH(String zhStr) {
        String result = "";
        if (!isNull(zhStr)) {
            if (zhStr.contains(".")) {
                String zhfStr = zhStr.substring(0, zhStr.indexOf("."));
                String zhaStr = zhStr.substring(zhStr.indexOf(".") + 1, zhStr.length());
                if (zhaStr.length() == 1) {
                    zhaStr += "00";
                } else if (zhaStr.length() == 2) {
                    zhaStr += "0";
                }
                result = "K" + zhfStr + "+" + zhaStr;
            } else {
                result = "K" + zhStr;
            }
        }
        return result;
    }
    public static int getWidth(Activity activity) {
        DisplayMetrics dm = new DisplayMetrics();
        activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
        int screenWidth = dm.widthPixels;
        return screenWidth;
    }
    public static String replaceNull(String str) {
        if (str == null||str.equals("")) {
            return "0";
        }
        return str;
    }
    /**
     * 判断定位是否可用
     *
     * @return {@code true}: 是<br>{@code false}: 否
     */
    public static boolean isLocationEnabled() {
        LocationManager lm = (LocationManager)Utils.getContext().getSystemService(Context.LOCATION_SERVICE);
        return lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER)
                || lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
    }
    /**
     * 获取ApplicationContext
     *
     * @return ApplicationContext
     */
    public static Context getContext() {
        if (context != null) return context;
        throw new NullPointerException("u should init first");
    }
}
